//
//  ViewController.m
//  英雄列表
//
//  Created by 伍银荣 on 2021/6/3.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "ViewController.h"
#import "Heros.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *herosArr;

@property (nonatomic, copy) NSString *text;

@end

@implementation ViewController

-(NSArray *)herosArr {
    if (!_herosArr) {
        NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]];
        NSMutableArray *mArr = [NSMutableArray array];
        for (NSDictionary *dict in arr) {
            Heros *heros = [Heros herosWithDionary:dict];
            [mArr addObject:heros];
        }
        _herosArr = mArr;
    }
    return _herosArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 60;
    
    self.tableView.delegate = self;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.herosArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Heros *model = self.herosArr[indexPath.row];
    static NSString *cellID = @"herosID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    cell.imageView.image = [UIImage imageNamed:model.icon];
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text = model.intro;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"当前点击第%ld行",indexPath.row);
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"编辑姓名" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        //点击取消按钮可以做的事情
        NSLog(@"点击了取消按钮");
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        //点击确定按钮需要做的事情
        NSLog(@"点击了确定按钮");
        //获取修改后的textfield的文本
        
        //将修改后的文本赋值给每一行cell
        Heros *model = self.herosArr[indexPath.row];
        model.name = self.text;
        //刷新tableView
       // [self.tableView reloadData];
        //刷新当前点击的这一行
        NSIndexPath *idxPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
        [self.tableView reloadRowsAtIndexPaths:@[idxPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }]];
    
   // UITextField *nameTF = alert.textFields.firstObject;
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        //获取当前点击的这一行的英雄的名字
        textField.text = [self.herosArr[indexPath.row] name];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//        self.text = textField.text;
        textField.delegate = self;
    }];
    
    
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - textFieldDelegate------
-(void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"停止编辑了---%@",textField.text);
    _text = textField.text;
}

//-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//    NSLog(@"string = %@",textField.text);
//    _text = textField.text;
//    return YES;
//}


@end
